const API = require("../../../utils/api")
const app = getApp()
// pages/home/apply/apply.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        clientCurrent:0,
        client:['业主','二手房东','物业','开发商'],
        typeCurrent:0,
        type:['厂房','土地','写字楼','仓库','商铺'],
        form:{
            bailor: '',
            putType: '',
            name: '',
            phone: '',
        },
        userInfo:'',
        agreement: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        this.get_statistics_num()
        this.setData({
            ['form.bailor']: that.data.client[that.data.clientCurrent],
            ['form.putType']: that.data.type[that.data.typeCurrent],
            userInfo:app.globalData.userInfo,
        })
    },
/**
     * 协议事件
     */
    agreementChange (event) {
        this.data.agreement = event.detail
    },
    /**
     * 接口 - 获取园区申请入驻人数
     */
    get_statistics_num:function(){
        let that = this
        API.get_statistics_num().then( res => {
            if( res.code == 200 ){
                this.setData({
                    statisticsNum: res.data.toString().split('')
                })
            }
        })
    },

    /**
     * 事件 - clientTabChange改变
     */
    clientTabChange:function(e){
        console.log(e.currentTarget)
        this.setData({
            clientCurrent: e.currentTarget.id,
            ['form.bailor']: e.currentTarget.dataset.text
        })
    },

    /**
     * 事件 - 投放类型tab改变
     */
    typeTabChange:function(e){
        this.setData({
            typeCurrent: e.currentTarget.id,
            ['form.putType']: e.currentTarget.dataset.text
        })
    },

    /**
     * 获取手机号
     */
    getPhoneNumber:function(e){
        let that = this
        if( app.globalData.userInfo.wxPhone ){
            return
        }
        wx.login({
        success (res) {
            if (res.code) {
                API.get_mobile({
                    appid:app.globalData.appid,
                    encryptedData:e.detail.encryptedData,
                    iv:e.detail.iv,
                    code:res.code
                },app.globalData.appid).then( res => {
                    if( res.code == 200 ){
                        that.setData({
                            phoneNumber: res.data.phoneNumber
                        })
                        app.globalData.userInfo.wxPhone = res.data.phoneNumber
                    }else{
                    wx.showToast({
                        title: '授权登陆失败，请稍后重试',
                        icon:'none'
                    })
                    }
                })
            }
        }
        })
    },

    /**
     * 表单提交
     */
    submit:function(e){
        if (!this.data.agreement) return wx.showToast({
            title: '请同意并阅读盈联易单配服务协议',
            icon: 'none'
        })
        let value = e.detail.value
        if( !value.name ){
            wx.showToast({
              title: '请填写姓名',
              icon:'none'
            })
            return
        }
        if( !value.phone ){
            wx.showToast({
              title: '请填写联系方式',
              icon:'none'
            })
            return
        }
        if( !value.gardenName ){
            wx.showToast({
              title: '请填写园区名称',
              icon:'none'
            })
            return
        }
        const userInfo = wx.getStorageSync('userInfo')
        let form = this.data.form
        form.name = value.name
        form.phone = value.phone
        form.gardenName = value.gardenName
        form.wxUserId = userInfo.id
        console.log(form)
        API.creat_enter_office(form).then( res => {
            if( res.code == 200 ){
                wx.redirectTo({
                  url: '/pages/index/set-success/set-success',
                })
            }else{
                wx.showToast({
                  title: '提交失败，请稍后重试',
                  icon:'none'
                })
            }
        })
    },
})